package com.ming.javaweb.servlet;

import com.ming.javaweb.beans.DTO.StudentDTO;
import com.ming.javaweb.beans.VO.StudentVO;
import com.ming.javaweb.beans.VO.TableVO;
import com.ming.javaweb.beans.entity.StudentDO;
import com.ming.javaweb.service.StudentService;
import com.ming.javaweb.service.impl.StudentServiceImpl;
import com.ming.javaweb.util.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {
    private StudentService studentService  = new StudentServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        if ("toStudentManage".equals(type)) {
//            查询出来学生列表放到request中
            String studentName = req.getParameter("studentName");
            String pageNowStr = req.getParameter("pageNow");
            System.out.println("我接收到的studentName："+studentName);
            System.out.println("我接收到的pageNow："+pageNowStr);
//            传入pageNowStr为空时，默认为第一页
            int pageNow = 1;
            if (pageNowStr != null && !"".equals(pageNowStr.trim())){
                pageNow = Integer.parseInt(pageNowStr);
            }
//            存入DTO层
            StudentDTO studentDTO = new StudentDTO();
            studentDTO.setStudentName(studentName);
            studentDTO.setPageNow(pageNow);

            try {
                TableVO<StudentVO> tableVO = studentService.queryStudentByPage(studentDTO);
                System.out.println("查看tableVO中的值："+ tableVO);
                tableVO.setPageNow(pageNow);
                tableVO.setStudentName(studentName);

//         放到request请求域中，并在studentManage.jsp中使用
                req.setAttribute("tableVO",tableVO);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

            req.getRequestDispatcher("/WEB-INF/student/studentManage.jsp").forward(req, resp);
        }else if ("toAdd".equals(type)){
            req.getRequestDispatcher("WEB-INF/student/add.jsp").forward(req,resp);
        }else if ("add".equals(type)){
//            执行学生的添加
            String studentName = req.getParameter("studentName");
            String no = req.getParameter("no");
            String birthDay = req.getParameter("birthDay");

            StudentDO studentDO = new StudentDO();
//            将http接收的数据存到DO中
            studentDO.setName(studentName);
            studentDO.setNo(no);

            if (!birthDay.equals("")){
                String format1 = String.format("yyyy-MM-dd HH:mm:ss", birthDay);
                System.out.println("不为空");
                studentDO.setBirthDay(DateUtil.convertStr2Date(birthDay));
            }else {
                Date date = new Date();
                SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = dateFormat.format(date);
                System.out.println(format);
                studentDO.setBirthDay(DateUtil.convertStr2Date(format));
            }

            try {
                Boolean aBoolean = studentService.addStudent(studentDO);
                if (aBoolean) {
                    resp.sendRedirect(req.getContextPath()+"/studentServlet?type=toStudentManage");
                }else {
                    req.getRequestDispatcher("WEB-INF/student/add.jsp").forward(req,resp);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }else if ("toUpdate".equals(type)) {
            String studentId = req.getParameter("id");
            try {
                StudentVO studentVO = studentService.getStudentBYId(Integer.parseInt(studentId));
                req.setAttribute("studentVO",studentVO);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            req.getRequestDispatcher("WEB-INF/student/update.jsp").forward(req,resp);
        }else if ("update".equals(type)) {
            String studentId = req.getParameter("studentId");
            String studentName = req.getParameter("studentName");
            String no = req.getParameter("no");
            String birthDay = req.getParameter("birthDay");
//            将http接收的数据存到DO中
            StudentDO studentDO = new StudentDO();
            studentDO.setId(Integer.parseInt(studentId));
            studentDO.setName(studentName);
            studentDO.setNo(no);
            studentDO.setBirthDay(DateUtil.convertStr2Date(birthDay));

            try {
                Boolean aBoolean = studentService.updateStudent(studentDO);
                if (aBoolean) {
                    resp.sendRedirect(req.getContextPath()+"/studentServlet?type=toStudentManage");
                }else {
                    req.getRequestDispatcher("WEB-INF/student/update.jsp").forward(req,resp);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }else if ("delete".equals(type)) {
            String id = req.getParameter("id");
            int deleteId = Integer.parseInt(id);
            try {
                studentService.deleteById(deleteId);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            resp.sendRedirect(req.getContextPath()+"/studentServlet?type=toStudentManage");
        }
    }
}
